#include <iostream>
using namespace std;

#define MAX 20

int n;
int ans, tmp;

struct Order
{
    int start;
    int end;
};

void f(Order orders[], int last)
{
    bool stop = true;
    for (int i = 1; i <= n; i++)
    {
        if (orders[i].start >= orders[last].end)
        {
            int inc = orders[i].end - orders[i].start;
            tmp += inc;
            f(orders, i);
            tmp -= inc;
            stop = false;
        }
    }
    if (stop)
        ans = (ans > tmp) ? ans : tmp;
}

int main()
{
    ans = tmp = 0;
    Order orders[MAX];
    orders[0].end = 0;

    cout << "一共有几份订单：";
    cin >> n;

    cout << "请输入" << n << "份订单：" << endl;
    for (int i = 1; i <= n; i++)
        cin >> orders[i].start >> orders[i].end;

    f(orders, 0);
    cout << "最长活动时间为：" << ans << endl;
    return 0;
}

/*
一共有几份订单：4
请输入4份订单：
1 2
3 5
1 4
4 5
最长活动时间为：4
*/